package utils;

import java.sql.*;

public class JDBCUtils {
    public static Connection getConnection() throws SQLException,ClassNotFoundException {
        String url="jdbc:mysql://localhost:3306/test";
        String user="root";
        String password="123456";
        Class.forName("com.mysql.jdbc.Driver");
        Connection connection= DriverManager.getConnection(url,user,password);
        return  connection;
    }

    public  static  void  release(Statement stmt,Connection connection){
        if(stmt!=null){
            try{
                stmt.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
            stmt=null;
        }
        if(connection!=null){
            try{
                connection.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
            connection=null;
        }
    }

    public  static  void  release( PreparedStatement stmt, Connection connection){
        if(stmt!=null){
            try{
                stmt.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
            stmt=null;
        }
        if(connection!=null){
            try{
                connection.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
            connection=null;
        }
    }

    public  static  void  release(Statement stmt, Connection connection, ResultSet resultSet){
        if(resultSet!=null){
            try{
                resultSet.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
            resultSet=null;
        }
        release(stmt,connection);
    }

    public  static  void  release(PreparedStatement stmt, Connection connection, ResultSet resultSet){
        if(resultSet!=null){
            try{
                resultSet.close();
            }catch (SQLException e){
                e.printStackTrace();
            }
            resultSet=null;
        }
        release(stmt,connection);
    }
}
